<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/20
 * Time: 13:45
 */

namespace app\common\model;


use think\Model;

class Article extends Model
{
    protected  $autoWriteTimestamp = true;

    public function getArticlesInfo()
    {
        $data = [
            'status' => ['neq',-1],
        ];
        $order=[
            'id' => 'desc',
        ];
        $result = $this -> where($data)
            -> order($order)
            -> paginate();
        return $result;

    }


    //添加到数据库
    public function add($data)
    {
        $data['status'] = 1;
        //作者
        $data['author'] =getAuthorId();
        //处理数据
        $data['tag'] = arr2str($data['tag']);
        //文章序列化
        $data['content'] = serialize($data['content']);

        $this -> allowField(true)-> save($data);
        return $this -> id ;
    }

    //前台获取先关文章信息
    public  function  getNormalArticles()
    {
        $order=[
            'id' => 'desc',
        ];

        //获取作者基本信息
        $result = $this ->  with([
            'articleAuthor' => function($query){
                $query -> field('username');
            }
        ])
            -> with([
                'articleTag' => function($query){
                    $query -> with('tagName')
                        -> field('name');
                }
            ])
            -> order($order)
            -> where('a.status = 1')
            -> select();

        //var_dump($result)
        return $result;

    }


    //获取内容详情
    public  function  getDetail($id)
    {

        //获取作者基本信息
        $result = $this ->  with([
            'articleAuthor' => function($query){
                $query -> field('username');
            }
        ])
            -> where('a.status = 1')
            -> find($id);


        //获取关键字信息
        $result['key'] = explode(",",$result['key']);

        $result['content'] = unserialize($result['content']);
        //var_dump($result);
        //上一篇和下一篇的信息
        $result['pre_next'] = $this -> next_pre($id);
        //var_dump($result);
        if ($result){
            $this -> updateReading($result['reading'],$id);
            return $result;
        }
    }

    //关联表
    public function articleAuthor()
    {
        return $this -> belongsTo('user','author','id',['article' => 'a']);
    }

   //获取标签相关信息
    public function articleTag()
    {
        return $this -> hasMany('ArticleTag','article','id');
    }

    //获取上下篇的信息
    public function next_pre($id)
    {
        $data['pre_id'] = $id +1;
        $data['pre_title'] = $this -> getArticleTitle($data['pre_id']);
        $data['next_id'] = $id -1;
        $data['next_title'] = $this -> getArticleTitle($data['next_id']);
        return $data;
    }

    //获取文章标题信息
    public function getArticleTitle($id)
    {
        $data =  $this -> field('title')->  find($id);
        return $data['title'];
    }

    //阅读量功能的添加
    public function updateReading($reading,$id)
    {
        $data['reading'] = $reading+1;
        $this -> where('id',$id) -> update($data);
    }

}